package com.ym.controller.soso;

import com.github.pagehelper.PageInfo;
import com.ym.base.enums.FlagEnum;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.soso.domain.entity.PharmacyInfoEntity;
import com.ym.soso.domain.param.PharmacyInfoQueryParam;
import com.ym.soso.domain.param.PharmacyInfoUpdateParam;
import com.ym.soso.domain.vo.PharmacyInfoListPcVO;
import com.ym.soso.domain.param.PublishParam;
import com.ym.soso.service.IPharmacyInfoSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.ym.common.constant.ValidRule.Add;
import static com.ym.common.constant.ValidRule.Update;

/**
 * 药店信息Controller
 * 
 * @author ym
 * @date 2020-09-02
 */
@Api(tags = "medical-药店信息")
@RestController("PharmacyInfoPcController")
@Validated
@RequestMapping("/medical/pharmacyInfo")
public class PharmacyInfoController extends BaseController {

    @Autowired
    private IPharmacyInfoSV pharmacyInfoSV;

    @ApiOperation("查询药店信息列表")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping("/list")
    public APIResponse<PageInfo<PharmacyInfoListPcVO>> list(@Valid PharmacyInfoQueryParam queryParam) {
        PageDomain.startPage();
        queryParam.setDeleted(FlagEnum.NO.getKey());
        List<PharmacyInfoListPcVO> list = pharmacyInfoSV.selectListPC(queryParam);
        return successPage(list);
    }

    @ApiOperation("获取药店信息详细信息")
    @Log(dataBase = false, printBody = false, printQuery = true)
    @GetMapping(value = "/info")
    public APIResponse<PharmacyInfoEntity> info(@RequestParam Long id) {
        PharmacyInfoEntity data = pharmacyInfoSV.selectById(id);
        return successData(data);
    }

    @ApiOperation("新增药店信息")
    @Log(printQuery = false)
    @PostMapping("/insert")
    public APIResponse insert(@RequestBody @Validated(value = Add.class) PharmacyInfoUpdateParam updateParam) {
        Long id = pharmacyInfoSV.insertPC(updateParam);
        return successData(id);
    }

    @ApiOperation("修改药店信息")
    @Log(printQuery = false)
    @PostMapping("/update")
    public APIResponse update(@RequestBody @Validated(value = Update.class) PharmacyInfoUpdateParam updateParam) {
        int result = pharmacyInfoSV.updateByIdPC(updateParam);
        return successData(result);
    }

    @ApiOperation("删除药店信息")
    @Log(printQuery = false)
    @PostMapping("/delete")
    public APIResponse delete(@RequestBody @Valid @NotNull Long[] ids) {
        pharmacyInfoSV.delete(ids);
        return success();
    }

    @ApiOperation("发布")
    @PostMapping(value = "/release", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public APIResponse release(
            @RequestBody @Valid PublishParam publishParams
    ) {
        pharmacyInfoSV.release(publishParams);

        return success();
    }
}
